import { IGetPaginationInfo, IGetResult, PaginationData } from 'src/utils/infomation';
import { ReturnResult } from './return-result';

export class PaginationReturnResult<T extends Array<any>> extends ReturnResult<T>
  implements IGetPaginationInfo<PaginationData<T>>, IGetResult
{
  // 当前页码
  private page: number;
  // 每页数量
  private pageSize: number;
  // 总条数
  private total: number;

  constructor(code: number, message: string, data: T) {
    super(code, message, data);

    this.page = 1;
    this.pageSize = 10;
    this.total = 0;
  }

  get totalPage(): number {
    return Math.ceil(this.total / this.pageSize);
  }

  public getPage() {
    return this.page;
  }

  public setPage(page: number) {
    this.page = page;
  }

  public getPageSize() {
    return this.pageSize;
  }

  public setPageSize(pageSize: number) {
    this.pageSize = pageSize;
  }

  public getTotal() {
    return this.total;
  }

  public setTotal(total: number) {
    this.total = total;
  }

  public getPaginationInfo() {
    return {
      page: this.getPage(),
      pageSize: this.getPageSize(),
      total: this.getTotal(),
      totalPage: this.totalPage,
      items: this.data,
    };
  }

  public getResult(): ({ code: number; message: string; data: any; }) {
    return {
      code: this.getCode(),
      message: this.getMessage(),
      data: this.data,
    };
  }

  public getPaginationResult() {
    return {
      code: this.getCode(),
      message: this.getMessage(),
      data: this.getPaginationInfo(),
    };
  }
}
